import VueRouter from "vue-router";
import Home from "@/pages/Home";
import Vue from "vue";
import Slide from "@/components/Slide";
import PoemList from "@/pages/PoemList";
import PoemItem from "@/pages/PoemItem";
import store from "@/store";
import AuthorList from "@/pages/AuthorList";
import AuthorItem from "@/pages/AuthorItem";
import LineList from "@/pages/LineList";
import FeaturedList from "@/pages/FeaturedList";
import FeaturedItem from "@/pages/FeaturedItem";
import NotFound from "@/pages/NotFound";
import SearchPoemAndLine from "@/pages/SearchPoemAndLine";

Vue.use(VueRouter);
const router = new VueRouter({
  routes: [
    {
      path: "/home",
      components: {
        default: Home,
        slide: Slide,
      },
      meta: {
        activeNav: 0,
        breadNumber: -1,
      },

      name: "首页",
    },
    {
      path: "/featuredList",
      name: "精选",
      components: {
        default: FeaturedList,
      },
      meta: {
        activeNav: 1,
        breadNumber: 0,
      },
    },
    {
      path: "/lineList",
      name: "名句",
      components: {
        default: LineList,
      },
      meta: {
        activeNav: 2,
        breadNumber: 0,
      },
    },

    {
      path: "/poemList/:aname?/:aid?",
      components: {
        default: PoemList,
      },
      meta: {
        activeNav: 3,
        breadNumber: 0,
      },
      name: "诗词",
    },
    {
      path: "/poemItem/:id",
      name: "诗词详情",
      components: {
        default: PoemItem,
      },
      meta: {
        father: "诗词",
        activeNav: 3,
        breadNumber: 1,
      },
    },
    {
      path: "/authorList",
      components: {
        default: AuthorList,
      },
      meta: {
        activeNav: 4,
        breadNumber: 0,
      },
      name: "诗人",
    },
    {
      path: "/authorItem/:id",
      name: "诗人详情",
      components: {
        default: AuthorItem,
      },
      meta: {
        father: "诗人",
        activeNav: 4,
        breadNumber: 1,
      },
    },
    {
      path: "/featuredItem",
      name: "精选个体",
      components: {
        default: FeaturedItem,
      },
      meta: {
        father: "精选",
        activeNav: 1,
        breadNumber: 1,
      },
    },
    {
      path: "/search",
      name: "搜索",
      components: {
        default: SearchPoemAndLine,
      },
      meta: {
        activeNav: 5,
        breadNumber: 0,
      },
    },

    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "*",
      redirect: "/404",
    },
    {
      path: "/404",
      name: "未定义",
      components: {
        default: NotFound,
      },
    },
  ],
});

function checkParmas(to, id) {
  let regAuthor = /authorItem/;
  let regPoem = /poemItem/;
  //检查诗人的id是否超出范围
  if (to.path.search(regAuthor)) {
    if (id > 10000) return true;
  } else if (to.path.search(regPoem)) {
    //检查诗词的id是否超出范围
    if (id > 10000) return true;
  }
  return false;
}

router.beforeEach((to, from, next) => {
  //回到顶部
  window, scrollTo(0, 0);
  //  清空面包屑
  store.commit("clearBreadLIst");
  // 如果是诗词或诗人页面，检查有没有这个诗词或诗人
  if (checkParmas(to, to.params.id) && from.path != "/404") {
    console.log(from.path != "/404");
    next("/404");
  }
  // 定向导航按钮
  store.commit("setActiveNav", to.meta.activeNav);

  let toBreadNumber = to.meta.breadNumber;
  //从“/"的情况
  //二层面包屑
  if (toBreadNumber == 0) {
    store.commit("setBread", { toBreadName: to.name });
    //三层面包屑
  } else if (toBreadNumber == 1) {
    store.commit("setBread", {
      //读取出发地route上的目的地 注意from
      toBreadName: from.meta.toBreadName,
      father: to.meta.father,
    });
  }
  //测试顺畅
  next();
});
export default router;
